/**
 ******************************************************************************
 * @file    task_lcd.cc
 * @author  summDy
 * @version V1.0.0
 * @date    25-06-2025
 * @brief   Brief description of the file
 ******************************************************************************
 * @attention
 *
 ******************************************************************************
 */

#include "task_lcd.h"

#define TAG "task_lcd"

/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */

esp_lcd_panel_io_handle_t panel_io  = nullptr;
esp_lcd_panel_handle_t    lcd_panel = nullptr;
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */



#if 0

void InitializeSpi() {
  spi_bus_config_t buscfg = {};
  buscfg.mosi_io_num      = DISPLAY_MOSI_PIN;
  buscfg.miso_io_num      = GPIO_NUM_NC;
  buscfg.sclk_io_num      = DISPLAY_CLK_PIN;
  buscfg.quadwp_io_num    = GPIO_NUM_NC;
  buscfg.quadhd_io_num    = GPIO_NUM_NC;
  buscfg.max_transfer_sz  = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
}

#if 0

void SpiLcdDisplay(
    esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, int width, int height, int offset_x, int offset_y,
    bool mirror_x, bool mirror_y, bool swap_xy, DisplayFonts fonts) {
  width_  = width;
  height_ = height;

  // draw white
  std::vector<uint16_t> buffer(width_, 0xF800);
  for (int y = 0; y < height_; y++) {
    esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
  }
  vTaskDelay(pdMS_TO_TICKS(2000));
  // Set the display to on
  ESP_LOGI(TAG, "Turning display on");
  ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));

  ESP_LOGI(TAG, "Initialize LVGL library");
  lv_init();

  ESP_LOGI(TAG, "Initialize LVGL port");
  lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  port_cfg.task_priority   = 1;
  lvgl_port_init(&port_cfg);

  ESP_LOGI(TAG, "Adding LCD screen");
  const lvgl_port_display_cfg_t display_cfg = {
      .io_handle      = panel_io_,
      .panel_handle   = panel_,
      .control_handle = nullptr,
      .buffer_size    = static_cast<uint32_t>(width_ * 10),
      .double_buffer  = false,
      .trans_size     = 0,
      .hres           = static_cast<uint32_t>(width_),
      .vres           = static_cast<uint32_t>(height_),
      .monochrome     = false,
      .rotation =
          {
              .swap_xy  = swap_xy,
              .mirror_x = mirror_x,
              .mirror_y = mirror_y,
          },
      .color_format = LV_COLOR_FORMAT_RGB565,
      .flags =
          {
              .buff_dma     = 1,
              .buff_spiram  = 0,
              .sw_rotate    = 0,
              .swap_bytes   = 1,
              .full_refresh = 0,
              .direct_mode  = 0,
          },
  };

  display_ = lvgl_port_add_disp(&display_cfg);
  if (display_ == nullptr) {
    ESP_LOGE(TAG, "Failed to add display");
    return;
  }

  if (offset_x != 0 || offset_y != 0) {
    lv_display_set_offset(display_, offset_x, offset_y);
  }

  // Update the theme
  if (current_theme_name_ == "dark") {
    current_theme = DARK_THEME;
  } else if (current_theme_name_ == "light") {
    current_theme = LIGHT_THEME;
  }

  SetupUI();
}

#endif

void InitializeLcdDisplay() {

  // 液晶屏控制IO初始化
  ESP_LOGD(TAG, "Install lcd_panel IO");
  esp_lcd_panel_io_spi_config_t io_config = {};
  io_config.cs_gpio_num                   = DISPLAY_CS_PIN;
  io_config.dc_gpio_num                   = DISPLAY_DC_PIN;
  io_config.spi_mode                      = DISPLAY_SPI_MODE;
  io_config.pclk_hz                       = 40 * 1000 * 1000;
  io_config.trans_queue_depth             = 10;
  io_config.lcd_cmd_bits                  = 8;
  io_config.lcd_param_bits                = 8;
  ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));

  // 初始化液晶屏驱动芯片
  ESP_LOGD(TAG, "Install LCD driver");
  esp_lcd_panel_dev_config_t panel_config = {};
  panel_config.reset_gpio_num             = DISPLAY_RST_PIN;
  panel_config.rgb_ele_order              = DISPLAY_RGB_ORDER;
  panel_config.bits_per_pixel             = 16;

  ESP_ERROR_CHECK(esp_lcd_new_panel_st7735(panel_io, &panel_config, &lcd_panel));

  esp_lcd_panel_reset(lcd_panel);
  vTaskDelay(pdMS_TO_TICKS(1000));
  esp_lcd_panel_init(lcd_panel);
  esp_lcd_panel_invert_color(lcd_panel, DISPLAY_INVERT_COLOR);
  esp_lcd_panel_swap_xy(lcd_panel, DISPLAY_SWAP_XY);
  esp_lcd_panel_mirror(lcd_panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
#ifdef LCD_TYPE_GC9A01_SERIAL
  panel_config.vendor_config = &gc9107_vendor_config;
#endif

#if 0
  display_ = new SpiLcdDisplay(
      panel_io,
      lcd_panel,
      DISPLAY_WIDTH,
      DISPLAY_HEIGHT,
      DISPLAY_OFFSET_X,
      DISPLAY_OFFSET_Y,
      DISPLAY_MIRROR_X,
      DISPLAY_MIRROR_Y,
      DISPLAY_SWAP_XY,
      {
          .text_font = &font_puhui_16_4,
          .icon_font = &font_awesome_16_4,
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
          .emoji_font = font_emoji_32_init(),
#else
          .emoji_font = DISPLAY_HEIGHT >= 240 ? font_emoji_64_init() : font_emoji_32_init(),
#endif
      });

#endif
}



#endif

void task_lcd_init(void) {
  // InitializeSpi();
  // InitializeLcdDisplay();

  // vTaskDelay(pdMS_TO_TICKS(1000));
  // std::vector<uint16_t> buffer(DISPLAY_WIDTH, 0xF800);
  // for (int y = 0; y < DISPLAY_HEIGHT; y++) {
  //   esp_lcd_panel_draw_bitmap(lcd_panel, 0, y, DISPLAY_WIDTH, y + 1, buffer.data());
  // }
}